package br.com.trumah.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.trumah.dao.interfaces.AssistenteSocialDAO;
import br.com.trumah.model.AssistenteSocial;
import br.com.trumah.model.Beneficiario;
import br.com.trumah.utils.HibernateUtil;

public class AssistenteSocialDAOImpl implements AssistenteSocialDAO {

	
	private Session session = null;
	private Transaction t = null;
	
	@Override
	public void salvar(AssistenteSocial assistenteSocial) {
		
		try {

			session = HibernateUtil.getSessionFactory().openSession();
			t = session.beginTransaction();
			session.save(assistenteSocial);
			t.commit();
			
		} catch (HibernateException ex) {
			
			t.rollback();
			ex.printStackTrace();
			
		} finally {
			
			session.close();
			
		}
		
	}

	@Override
	public AssistenteSocial obterAssistenteSocial(long id) {
		
		AssistenteSocial assistenteSocial = null;
		
		try {
			
			session = HibernateUtil.getSessionFactory().openSession();
			assistenteSocial = (AssistenteSocial) session.load(AssistenteSocial.class, id);
			
		} catch (HibernateException ex) {
			
			ex.printStackTrace();
			
		} finally {
			
			session.close();
			
		}
		
		return assistenteSocial;
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AssistenteSocial> listar() {
		
		List<AssistenteSocial> lista = null;
		
		try {

			session = HibernateUtil.getSessionFactory().openSession();
			t = session.beginTransaction();
			lista = session.createQuery("from AssistenteSocial").list();
			t.commit();
			
		} catch (HibernateException ex) {
			
			ex.printStackTrace();
			
		} finally {
			
			session.close();
			
		}
		
		return lista;
	}
	

	@Override
	public void remover(AssistenteSocial assistenteSocial) {
		
		try {

			session = HibernateUtil.getSessionFactory().openSession();
			t = session.beginTransaction();
			session.delete(assistenteSocial);
			t.commit();
			
		} catch (HibernateException ex) {
			
			t.rollback();
			ex.printStackTrace();
			
		} finally {
			
			session.close();
			
		}
		
	}

	@Override
	public void atualizar(AssistenteSocial assistenteSocial) {

		try {

			session = HibernateUtil.getSessionFactory().openSession();
			t = session.beginTransaction();
			session.update(assistenteSocial);
			t.commit();
			
		} catch (HibernateException ex) {
			
			t.rollback();
			ex.printStackTrace();
			
		} finally {
			
			session.close();
			
		}
		
	}

}
